let oldArrayProtoMethods = Array.prototype

export let ArrayMethods = Object.create(oldArrayProtoMethods)

let methods = [
    "push",
    "pop",
    "unshift",
    "shift",
    "splice"
]

methods.forEach(item => {
    ArrayMethods[item] = function(...args) {
        console.log("函数劫持")
        let result = oldArrayProtoMethods[item].apply(this, args)
        let inserted
        switch(item) {
            case 'push':
            case 'unshift':
                inserted = args
                break;
            case 'splice':
                inserted = args.splice(2); // arr.splice(0, 1, {a: 6})
                break;
        }
        console.log(this)
        let ob = this.__ob__
        if(inserted) {
            ob.observeArray(inserted) // 对我们添加的对象进行劫持
        }
        return result
    }
})